<template>
  <view>
    <my-search></my-search>
    <view class="scroll-view-contaioner">
      <!-- 左侧滚动盒子 -->
      <scroll-view scroll-y="true" :style="{ height: wh + 'px' }" class="left-scroll-view">
        <view :class="['left-scroll-view-item', i === active ? 'active' : '']" v-for="(cate, i) in cateList" :key="i" @click="getFinlay(cate.cat_id, i)">{{ cate.cat_name }}</view>
      </scroll-view>
      <!-- 右侧滚动盒子 -->
      <!-- <scroll-view scroll-y="true" style="height:100vh" class="right-scroll-view" :scroll-top="active? 0 : 0 "> -->
      <scroll-view scroll-y="true" style="height:100vh" class="right-scroll-view" :scroll-top="scrollTop">
        <!-- 二级分类 -->
        <view v-for="(leve2, i2) in cateLeve2" :key="i2" class="cate-level2">
          /&nbsp;{{ leve2.cat_name }}&nbsp;/
          <!-- 三级分类 -->
          <view class="cate-level3">
            <view class="cate-leve3-item" v-for="(leve3, i3) in leve2.children" :key="i3" @click="gotoGoodList(leve3)">
              <image :src="leve3.cat_icon" mode="widthFix" class="leve3-img"></image>
              <text class="leve3-text">{{ leve3.cat_name }}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import badgeMix from '@/mixins/tabbar_badge.js';
export default {
  mixins: [badgeMix],
  data() {
    return {
      wh: 0,
      cateList: [],
      active: 0,
      // 二级分类数据
      cateLeve2: [],
      scrollTop: 0
    };
  },
  onLoad() {
    const sysInfo = uni.getSystemInfoSync();
    this.wh = sysInfo.windowHeight - 50;
    this.getCateList();
  },
  methods: {
    async getCateList() {
      const { data: res } = await uni.$http.get('/api/public/v1/categories');
      if (res.meta.status !== 200) return uni.$showMsg();
      // 初始化时将索引为0的二级分类赋值
      this.cateLeve2 = res.message[0].children;
      this.cateList = res.message;
    },
    getFinlay(id, i) {
      this.active = i;
      // 通过点击动态赋值二级分类
      this.scrollTop = 1 - this.scrollTop;
      this.cateLeve2 = this.cateList[i].children;
    },
    gotoGoodList(leve3) {
      uni.navigateTo({
        url: '/subpkg/goods_list/goods_list?cid=' + leve3.cat_id
      });
    }
  }
};
</script>

<style lang="scss">
.scroll-view-contaioner {
  display: flex;
  .left-scroll-view {
    width: 120px;
    .left-scroll-view-item {
      background-color: #f7f7f7;
      line-height: 60px;
      font-size: 12px;
      text-align: center;
      &.active {
        background-color: #ffffff;
        position: relative;
        &::before {
          content: '';
          display: block;
          position: absolute;
          width: 3px;
          height: 30px;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
          background-color: #c00000;
        }
      }
    }
  }
}
.cate-level2 {
  text-align: center;
  font-size: 16px;
  font-weight: 700;
  margin: 15px 0;
}
.cate-level3 {
  display: flex;
  flex-wrap: wrap;
  .cate-leve3-item {
    flex: 33.33%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    margin-bottom: 10px;
  }
  .leve3-img {
    width: 60px;
    height: 60px;
  }
  .leve3-text {
    font-size: 12px;
  }
}
</style>
